<template>
  <el-container class="whole">
    <el-header
      height="50px"
      style="
        background-color: #313131;
        color: white;
        font-size: 30px;
        line-height: 50px;
      "
      >求索问卷--吾将上下而求索<span style="float: right; margin-right: 80px"
        ><el-button type="text" @click="savePdf" style="font-size: 20px"
          >导出PDF</el-button
        ></span
      ></el-header
    >
    <el-main class="body">
      <div id="exportPdf" ref="exportPdf">
        <div class="qrcode">
          <vue-qr
            text="http://106.12.141.103:8080/index.html#/"
            :size="70"
            :margin="5"
          ></vue-qr>
        </div>
        <div class="date">
          <div>发布者: {{ survey.author }}</div>
          <div>发布时间: {{ convertTime(new Date()) }}</div>
          <div v-if="survey.deadline !== null">
            截止时间: {{ convertTime(new Date()) }}
          </div>
        </div>
        <div class="title">{{ survey.title }}</div>
        <div class="description">{{ survey.description }}</div>
        <div
          v-for="(item, index) in questions"
          :key="index"
          style="border-top: 1px solid #e0e0e0; padding-bottom: 15px"
        >
          <div
            style="
              margin: 0 auto;
              width: 80%;
              padding-top: 30px;
              padding-bottom: 10px;
            "
          >
            <span v-if="!survey.numHidden">第 {{ index + 1 }} 题</span>
            (<span v-if="item.type === 0">单选</span>
            <span v-if="item.type === 1">多选</span>
            <span v-if="item.type === 3">填空</span>
            <span v-if="item.type === 2">评分</span>
            <span v-if="item.type === 5">报名</span>
            <span v-if="item.type === 4">投票</span>
            <span v-if="item.type === 6">定位</span>
            <span v-if="[1, 4, 5].indexOf(item.type) >= 0"
              ><span v-if="item.propJson.min == item.propJson.max">{{
                item.propJson.min
              }}</span
              ><span v-else
                >{{ item.propJson.min }}~{{ item.propJson.max }}</span
              >个</span
            >)<span
              v-if="
                survey.type == 1 &&
                [0, 1].indexOf(item.type) >= 0 &&
                item.score > 0
              "
              >({{ item.score }}分)</span
            >
            <span v-if="item.required === true" style="color: red">(必选)</span>
          </div>
          <div class="questions">{{ item.stem }}</div>
          <div
            class="questions_description"
            style="color: #999999; font-size: 10px"
          >
            {{ item.description }}
          </div>
          <div v-if="item.type === 0" class="questions">
            <el-radio-group v-model="answer['' + index]">
              <el-radio
                v-for="(choice, choice_index) in item.propJson.options"
                :key="choice_index"
                style="display: block; margin: 25px 0"
                :label="choice_index"
                >{{ item.propJson.options[choice_index] }}</el-radio
              >
            </el-radio-group>
          </div>
          <div v-if="item.type === 1 || item.type === 4" class="questions">
            <el-checkbox-group v-model="answer['' + index]">
              <el-checkbox
                v-for="(choice, choice_index) in item.propJson.options"
                :key="choice_index"
                style="display: block; margin: 25px 0"
                :label="choice_index"
                >{{ item.propJson.options[choice_index] }}
              </el-checkbox>
            </el-checkbox-group>
          </div>
          <div v-if="item.type === 3" class="questions">
            <el-input
              v-model="answer['' + index]"
              type="textarea"
              autosize
              placeholder="在这里填写回答..."
              style="margin: 25px 0"
            ></el-input>
          </div>
          <div v-if="item.type === 2" class="questions">
            <el-rate style="margin: 25px 0" v-model="answer[index]"></el-rate>
          </div>
          <div v-if="item.type === 5" class="questions">
            <el-checkbox-group v-model="answer['' + index]">
              <el-checkbox
                v-for="(choice, choice_index) in item.propJson.options"
                :key="choice_index"
                style="display: block; margin: 25px 0; width: 420px"
                :label="choice_index"
                @change="print(choice_index)"
                >{{ item.propJson.options[choice_index] }}
                <div style="display: inline-block; padding: 0 0 0 100px">
                  <span v-if="item.propJson.limit[choice_index] == 0"
                    >无人数限制</span
                  ><span v-else
                    >总人数:{{ item.propJson.limit[choice_index] }} 剩余人数:{{
                      item.propJson.limit[choice_index]
                    }}</span
                  >
                </div>
              </el-checkbox>
            </el-checkbox-group>
          </div>
          <div v-if="item.type === 6" class="questions">
            <div style="margin: 25px 0">
              <el-input
                style="display: inline-block; width: 400px"
                disabled
              ></el-input>
              <el-button @click="getLocate(index)">点击获取当前位置</el-button>
            </div>
          </div>
        </div>
        <div style="text-align: center; padding: 30px 0">
          <el-button type="primary" @click="submit" style="width: 120px"
            >提 交</el-button
          >
        </div>
      </div>
    </el-main>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      survey: {},
      questions: [],
      answer: {},
    };
  },
  methods: {
    outputAnswerSheet(questions) {
      var answerSheet = {};
      for (var i = 0; i < questions.length; i++) {
        if ([1, 4, 5].indexOf(questions[i].type) >= 0) {
          answerSheet["" + i] = [];
        } else if (questions[i].type === 2) {
          answerSheet["" + i] = 0;
        } else if ([0, 3].indexOf(questions[i].type) >= 0) {
          answerSheet["" + i] = null;
        }
      }
      return answerSheet;
    },
    submit() {
      alert("预览问卷模拟提交...");
    },
    convertTime(timestampe) {
      var time = new Date(timestampe);
      var y = time.getFullYear();
      var m = time.getMonth() + 1;
      var d = time.getDate();
      var h = time.getHours();
      var mm = time.getMinutes();
      var s = time.getSeconds();
      return (
        y +
        "-" +
        this.add0(m) +
        "-" +
        this.add0(d) +
        "-" +
        this.add0(h) +
        ":" +
        this.add0(mm) +
        ":" +
        this.add0(s)
      );
    },
    add0(m) {
      return m < 10 ? "0" + m : m;
    },
    mixed(arr) {
      for (var i = 0; i < arr.length; i++) {
        var iRand = parseInt(arr.length * Math.random());
        var temp = arr[i];
        arr[i] = arr[iRand];
        arr[iRand] = temp;
      }
      return arr;
    },
    init() {
      this.survey = JSON.parse(localStorage.getItem("survey"));
      localStorage.removeItem("survey");
      if (this.survey.mixed)
        this.survey.questions = this.mixed(this.survey.questions);
      this.answer = this.outputAnswerSheet(this.survey.questions);
      this.questions = this.survey.questions;
    },
    savePdf() {
      this.$PDFSave(this.$refs.exportPdf, this.survey.title + "-求索问卷");
    },
  },
  created() {
    this.init();
    // console.log(JSON.stringify(this.survey));
  },
};
</script>


<style lang="less" scoped>
.whole {
  height: 100%;
  width: 100%;
  background-color: #f5f5f5;
}
.body {
  height: 100%;
  width: 50%;
  margin: 0 auto;
  background-color: #fff;
  // overflow: auto;
}
// /deep/.el-main {
//   overflow: unset;
// }
.title {
  text-align: center;
  padding: 30px 0 20px 0;
  font-size: 30px;
  max-width: 40%;
  margin: 0 auto;
}
.qrcode {
  float: left;
}
.date {
  float: right;
  font-size: 12px;
  padding-right: 10px;
  padding-top: 10px;
  color: #5a5a5a;
}
.description {
  text-align: center;
  font-size: 15px;
  max-width: 60%;
  margin: 0 auto;
  padding-bottom: 20px;
}
.questions {
  width: 80%;
  margin: 0 auto;
}
</style>